'use strict'

class manage {
    constructor () {

    }
    // __mid(){// 启用token-verify中间件
    //     return [
    //       {
    //         name:'token-verify',
    //         path:['list','get','post','delete']
    //       }
    //     ]
    // }
    async list(c) { //  活动列表展示路由
        let sqltext = `SELECT * FROM activity ORDER BY "activityId" DESC`
        try {
            let r = await c.service.psql.query(sqltext,[]);
            c.send(JSON.stringify(r.rows))
            
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async get(c) { //单个活动展示路由
        let sqltext = `SELECT * FROM activity WHERE "activityId"=$1`
        try {
            let r = await c.service.psql.query(sqltext,[c.param.id]);
            if (r.rowCount != 0) { 
                c.send(JSON.stringify(r.rows))
            } else {
                c.status(500).send('Show activity failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async post(c) { //活动更新/添加路由
        console.log('执行了post manage');
        // 此处应写一个判断，即如果有图片则需要去保存图片且保存图片路径
        let isUpdate = c.headers.update;
        console.log("isUpdate",isUpdate);
        if (isUpdate == 'true') { //更新活动
            
            // let f = c.getFile('image');
            // console.log('f：',f)
            // console.log('!f：',!f)
            // console.log('执行了更新活动')
            // if (!f) { //未获取到图片
                
            //     // 保存文字
            //     try {
            //         let sqltext = `UPDATE activity SET "shortTitle"=$1,"activityTime"=$2,"activitySpot"=$3,`
            //                         +`"activityContent"=$4,"fullTitle"=$5 WHERE "activityId"=$6`;
            //         // console.log(c)
            //         console.log('----------------------------------------------------------------')
            //         console.log(c.body)
            //         console.log('----------------------------------------------------------------')
            //         console.log('c.body.toString()：',c.body.toString())
            //         let data = JSON.parse(c.body.toString());
            //         console.log(typeof data)
            //         console.log('标志1')
            //         console.log(typeof data)
            //         console.log(data);
            //         let startTime = data.activityTime[0].split('T')[0].split('-');
            //         let endTime = data.activityTime[1].split('T')[0].split('-');
            //         for (let j = 0; j < 3; j++) {
            //             if (j == 0) {
            //                 startTime[j] = startTime[j] + '年';
            //                 endTime[j] = endTime[j] + '年'
            //             }
            //             else if (j == 1) {
            //                 startTime[j] = startTime[j] + '月';
            //                 endTime[j] = endTime[j] + '月'
            //             }
            //             else{
            //                 startTime[j] = startTime[j] + '日';
            //                 endTime[j] = endTime[j] + '日'
            //             } 
            //         }
            //         startTime = startTime.join("");
            //         endTime = endTime.join("");
            //         let timeStr = startTime+"到"+endTime;
            //         console.log("timeStr：",timeStr)
            //         let r = await c.service.psql.query(sqltext,[data.shortTitle,timeStr,data.activitySpot,data.activityContent,data.fullTitle,data.activityId]);
            //         console.log('标志2')
            //         console.log(r)
            //         if (r.rowCount != 0) { 
            //             c.send('Add activity successfully!')
            //         } else {
            //             c.status(500).send('Add activity failed!')
            //         }
            //     } catch (err) {
            //         c.status(500).send("Database's operations failed!")
            //     }
            // }
            // else{ //获取到图片
            //     // console.log(c)
            //     console.log('----------------------------------------------------------------')
            //     console.log('c.body：',c.body)
            //     console.log('----------------------------------------------------------------')
            //     console.log('c.body.toString()：',c.body.toString())
            //     // 保存图片文件
            //     // let fname = c.helper.makeName(f.filename);
            //     // let filePath = `${c.service.imagePath}/${fname}`;
            //     // await c.moveFile(f,filePath);
            //     // 保存文字和图片路径
                
            // }
            try {
                    
                let data = JSON.parse(c.body.toString());
                console.log(data);
                let sqltext = `UPDATE activity SET "shortTitle"=$1,"activityTime"=$2,`
                            +`"activitySpot"=$3,"activityContent"=$4,"activityImg"=$5,"fullTitle"=$6 WHERE "activityId"=$7`;
                let startTime = data.activityTime[0].split('T')[0].split('-');
                let endTime = data.activityTime[1].split('T')[0].split('-');
                for (let j = 0; j < 3; j++) {
                    if (j == 0) {
                        startTime[j] = startTime[j] + '年';
                        endTime[j] = endTime[j] + '年'
                    }
                    else if (j == 1) {
                        startTime[j] = startTime[j] + '月';
                        endTime[j] = endTime[j] + '月'
                    }
                    else{
                        startTime[j] = startTime[j] + '日';
                        endTime[j] = endTime[j] + '日'
                    } 
                }
                startTime = startTime.join("");
                endTime = endTime.join("");
                let timeStr = startTime+"到"+endTime;
                console.log("timeStr：",timeStr)
                let r = await c.service.psql.query(sqltext,[data.shortTitle,timeStr,data.activitySpot,data.activityContent,data.activityImg,data.fullTitle,data.activityId]);
                console.log('标志2')
                console.log(r);
                if (r.rowCount != 0) { 
                    c.send('Add activity successfully!')
                } else {
                    c.status(500).send('Add activity failed!')
                }
            } catch (err) {
                c.status(500).send("Database's operations failed!")
            }
        }
        else{ // 添加活动
            // console.log('执行了添加活动')
            // let f = c.getFile('image');
            // console.log('f：',f)
            // console.log('!f：',!f)
            // if (!f) { //未获取到图片
            //     let sqltext = `INSERT INTO activity("shortTitle","activityTime","activitySpot","activityContent")`  
            //                 +` VALUES($1,$2,$3,$4)`;
            //     // 保存文字
            //     try {
            //         // console.log(c)
            //         console.log('----------------------------------------------------------------')
            //         console.log(c.body)
            //         console.log('----------------------------------------------------------------')
            //         console.log('c.body.toString()：',c.body.toString())
            //         let data = JSON.parse(c.body.toString());
            //         console.log('标志1')
            //         console.log(data);
            //         let startTime = data.activityTime[0].split('T')[0].split('-');
            //         let endTime = data.activityTime[1].split('T')[0].split('-');
            //         for (let j = 0; j < 3; j++) {
            //             if (j == 0) {
            //                 startTime[j] = startTime[j] + '年';
            //                 endTime[j] = endTime[j] + '年'
            //             }
            //             else if (j == 1) {
            //                 startTime[j] = startTime[j] + '月';
            //                 endTime[j] = endTime[j] + '月'
            //             }
            //             else{
            //                 startTime[j] = startTime[j] + '日';
            //                 endTime[j] = endTime[j] + '日'
            //             } 
            //         }
            //         startTime = startTime.join("");
            //         endTime = endTime.join("");
            //         let timeStr = startTime+"到"+endTime;
            //         console.log("timeStr：",timeStr) 
            //         let r = await c.service.psql.query(sqltext,[data.shortTitle,timeStr,data.activitySpot,data.activityContent]);
            //         console.log('标志2')
            //         console.log(r)
            //         if (r.rowCount != 0) { 
            //             c.send('Add activity successfully!')
            //         } else {
            //             c.status(500).send('Add activity failed!')
            //         }
            //     } catch (err) {
            //         c.status(500).send("Database's operations failed!")
            //     }
            // }
            // else{ //获取到图片
            //     // console.log(c)
            //     console.log('----------------------------------------------------------------')
            //     console.log('c.body：',c.body)
            //     console.log('----------------------------------------------------------------')
            //     console.log('c.body.toString()：',c.body.toString())
            //     // 保存图片文件
            //     // let fname = c.helper.makeName(f.filename);
            //     // let filePath = `${c.service.imagePath}/${fname}`;
            //     // await c.moveFile(f,filePath);
            //     // 保存文字和图片路径
                
            // }
            try {
                    
                let data = JSON.parse(c.body.toString());
                console.log(data);
                let sqltext = `INSERT INTO activity("shortTitle","activityImg","activityTime","activitySpot","activityContent","fullTitle")`  // 此处应写一个判断，即如果有图片则需要去保存图片且保存图片路径
                            +` VALUES($1,$2,$3,$4,$5,$6)`;
                let startTime = data.activityTime[0].split('T')[0].split('-');
                let endTime = data.activityTime[1].split('T')[0].split('-');
                for (let j = 0; j < 3; j++) {
                    if (j == 0) {
                        startTime[j] = startTime[j] + '年';
                        endTime[j] = endTime[j] + '年'
                    }
                    else if (j == 1) {
                        startTime[j] = startTime[j] + '月';
                        endTime[j] = endTime[j] + '月'
                    }
                    else{
                        startTime[j] = startTime[j] + '日';
                        endTime[j] = endTime[j] + '日'
                    } 
                }
                startTime = startTime.join("");
                endTime = endTime.join("");
                let timeStr = startTime+"到"+endTime;
                console.log("timeStr：",timeStr)
                let r = await c.service.psql.query(sqltext,[data.shortTitle,data.activityImg,timeStr,data.activitySpot,data.activityContent,data.fullTitle]);
                console.log('标志2')
                console.log(r)
                if (r.rowCount != 0) { 
                    c.send('Add activity successfully!')
                } else {
                    c.status(500).send('Add activity failed!')
                }
            } catch (err) {
                c.status(500).send("Database's operations failed!")
            }
        
        }

        
    }
    async delete(c) { //活动删除路由
        let sqltext = `DELETE FROM activity WHERE "activityId"=$1`
        try {
            let r = await c.service.psql.query(sqltext,[c.param.id]);
            if (r.rowCount != 0) { 
                c.send(JSON.stringify('Delete activity successfully!'))
            } else {
                c.status(500).send('Delete activity failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }

}
module.exports = manage;